import copy
import codecs
from datetime import datetime

#+--------+--------+--------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+-----------+-----------+-------------+
#| 0.日期 | 1.名称 | 2.代码 | 3.开盘价 | 4.最高价 | 5.最低价 | 6.收盘价 | 7.前收盘 | 8.涨跌额 | 9.涨跌率 | 10.换手率 | 11.成交量 | 12.成交额 | 13.总市值 | 14.流通市值 |
#+--------+--------+--------+----------+----------+----------+----------+----------+----------+----------+-----------+-----------+-----------+-----------+-------------+

class data_processing():

    def open_file():

        file = open('./000422.csv','r',encoding='gb2312')

        return file

    def row_to_list():

        file = data_processing.open_file()

        source_data = []

        for i in file:

            str1 = i.replace('\n','')

            str2 = str1.split(',')

            source_data.append(str2)

        return source_data

    # ----------------------------------------------
    # 源按日数据调整为标准列顺序(带0值)
    # ----------------------------------------------
    def source_day_data():

        source_data = data_processing.row_to_list()

        day_data = []

        # 删除表头
        del source_data[0]

        source_data.reverse()

        temp_close = []

        temp_data_row = []

        for i in range(0, len(source_data)):

            if float(source_data[i][11]) != 0:
                temp_close.append(round(float(source_data[i][3]), 4))
            else:
                pass

            temp_data_row.insert(0, source_data[i][0])#0.日期
            temp_data_row.insert(1, source_data[i][2])#1.名称
            temp_data_row.insert(2, source_data[i][1])#2.代码
            temp_data_row.insert(3, round(float(source_data[i][6]), 4))#3.开盘价
            temp_data_row.insert(4, round(float(source_data[i][4]), 4))#4.最高价
            temp_data_row.insert(5, round(float(source_data[i][5]), 4))#5.最低价
            temp_data_row.insert(6, round(float(source_data[i][3]), 4))#6.收盘价

            # [7.前收盘]判断是否是第一行，如果是第一行，前收盘为当日开盘价。
            if i == 0:
                temp_data_row.insert(7, round(float(source_data[i][6]), 4))
            elif i != 0 and float(source_data[i][11]) != 0 and float(source_data[i-1][11]) == 0:
                temp_data_row.insert(7, temp_close[-2])
            elif i != 0 and float(source_data[i][11]) != 0 and float(source_data[i-1][11]) != 0:
                temp_data_row.insert(7, round(float(source_data[i-1][3]), 4))
            elif i != 0 and float(source_data[i][11]) == 0 and float(source_data[i-1][11]) == 0:
                temp_data_row.insert(7, round(float(0), 4))
            elif i != 0 and float(source_data[i][11]) == 0 and float(source_data[i-1][11]) != 0:
                temp_data_row.insert(7, round(float(0), 4))
            else:
                pass

            # [8.涨跌额 & 9.涨跌率]判断收盘价减去前收盘是否为0，如果为0，则当日涨跌额为0，涨跌率为0。
            if temp_data_row[6] - temp_data_row[7] == 0:
                temp_data_row.insert(8, round(float(0), 4))
                temp_data_row.insert(9, round(float(0), 4))
            else:
                temp_data_row.insert(8, round((temp_data_row[6] - temp_data_row[7]), 4))
                temp_data_row.insert(9, round((temp_data_row[8] / temp_data_row[7]), 4))

            temp_data_row.insert(10, round(float(source_data[i][10]) * 0.01, 4))#10.换手率
            temp_data_row.insert(11, round(float(source_data[i][11]), 4))#11.成交量
            temp_data_row.insert(12, round(float(source_data[i][12]), 4))#12.成交额
            temp_data_row.insert(13, round(float(source_data[i][13]), 4))#13.总市值
            temp_data_row.insert(14, round(float(source_data[i][14]), 4))#14.流通市值

            temp_data_row_i = copy.deepcopy(temp_data_row)
            day_data.append(temp_data_row_i)

            temp_data_row.clear()

        return day_data

    # ----------------------------------------------
    # 待定函数
    # ----------------------------------------------
    def day_data_remove_0_value():

        day_data = data_processing.source_day_data()

        day_data_r0v = []

        for i in day_data:

            if i[11] != 0:

                day_data_r0v.append(i)

        return day_data_r0v

    # ----------------------------------------------
    # 将按日数据转换为按周数据(避免计算错误/选择不带0值的按日数据)
    # ----------------------------------------------
    def day_to_week():

        day_data_r0v = data_processing.day_data_remove_0_value()

        week_data = []

        new_data_row = []

        temp_date = []
        temp_name = '湖北宜化'
        temp_code = "'000422"
        temp_open = []
        temp_price = []
        temp_close = []
        # 前收盘用previousclose代替
        temp_previousclose = []
        # 换手率用turn代替
        temp_turn = []
        # 成交量用volume代替
        temp_volume = []
        # 成交额用totalmoney代替
        temp_totalmoney = []
        # 总市值用totalmarketcap代替
        temp_totalmarketcap = []
        # 流通值用circulationmarketcap代替
        temp_circulationmarketcap = []

        for i in range(0, len(day_data_r0v)):

            if i == 0:

                # 把日期加入临时列表
                temp_date.append(day_data_r0v[i][0])
                # 把开盘价加入临时列表
                temp_open.append(float(day_data_r0v[i][3]))
                # 把最高价，最低价加入临时列表
                temp_price.append(float(day_data_r0v[i][4]))
                temp_price.append(float(day_data_r0v[i][5]))
                # 把收盘价加入临时列表
                temp_close.append(float(day_data_r0v[i][6]))
                # 把换手率加入临时列表
                temp_turn.append(float(day_data_r0v[i][10]))
                # 把成交量加入临时列表
                temp_volume.append(float(day_data_r0v[i][11]))
                # 把成交额加入临时列表
                temp_totalmoney.append(float(day_data_r0v[i][12]))
                # 把总市值加入临时列表
                temp_totalmarketcap.append(float(day_data_r0v[i][13]))
                # 把流通市值加入临时列表
                temp_circulationmarketcap.append(float(day_data_r0v[i][14]))

            # 2. 判断当前日期周号大于前一天，并且前一天数据不在临时列表内
            elif datetime.weekday(datetime.strptime(day_data_r0v[i][0], "%Y-%m-%d")) > datetime.weekday(datetime.strptime(day_data_r0v[i-1][0], "%Y-%m-%d")):

                # 前一天数据不在临时列表内
                if day_data_r0v[i-1][0] not in temp_date:

                    # 把日期加入临时列表
                    temp_date.append(day_data_r0v[i-1][0]) ; temp_date.append(day_data_r0v[i][0])
                    # 把开盘价加入临时列表
                    temp_open.append(float(day_data_r0v[i-1][3])) ; temp_open.append(float(day_data_r0v[i][3]))
                    # 把最高价，最低价加入临时列表
                    temp_price.append(float(day_data_r0v[i-1][4])) ; temp_price.append(float(day_data_r0v[i][4]))
                    temp_price.append(float(day_data_r0v[i-1][5])) ; temp_price.append(float(day_data_r0v[i][5]))
                    # 把收盘价加入临时列表
                    temp_close.append(float(day_data_r0v[i-1][6])) ; temp_close.append(float(day_data_r0v[i][6]))
                    # 把换手率加入临时列表
                    temp_turn.append(float(day_data_r0v[i-1][10])) ; temp_turn.append(float(day_data_r0v[i][10]))
                    # 把成交量加入临时列表
                    temp_volume.append(float(day_data_r0v[i-1][11])) ; temp_volume.append(float(day_data_r0v[i][11]))
                    # 把成交额加入临时列表
                    temp_totalmoney.append(float(day_data_r0v[i-1][12])) ; temp_totalmoney.append(float(day_data_r0v[i][12]))
                    # 把总市值加入临时列表
                    temp_totalmarketcap.append(float(day_data_r0v[i-1][13])) ; temp_totalmarketcap.append(float(day_data_r0v[i][13]))
                    # 把流通市值加入临时列表
                    temp_circulationmarketcap.append(float(day_data_r0v[i-1][14])) ; temp_circulationmarketcap.append(float(day_data_r0v[i][14]))

                # 前一天数据在临时列表内
                elif day_data_r0v[i-1][0] in temp_date:

                    # 把日期加入临时列表
                    temp_date.append(day_data_r0v[i][0])
                    # 把开盘价加入临时列表
                    temp_open.append(float(day_data_r0v[i][3]))
                    # 把最高价，最低价加入临时列表
                    temp_price.append(float(day_data_r0v[i][4]))
                    temp_price.append(float(day_data_r0v[i][5]))
                    # 把收盘价加入临时列表
                    temp_close.append(float(day_data_r0v[i][6]))
                    # 把换手率加入临时列表
                    temp_turn.append(float(day_data_r0v[i][10]))
                    # 把成交量加入临时列表
                    temp_volume.append(float(day_data_r0v[i][11]))
                    # 把成交额加入临时列表
                    temp_totalmoney.append(float(day_data_r0v[i][12]))
                    # 把总市值加入临时列表
                    temp_totalmarketcap.append(float(day_data_r0v[i][13]))
                    # 把流通市值加入临时列表
                    temp_circulationmarketcap.append(float(day_data_r0v[i][14]))

                else:

                    pass

            # 3. 判断当前日期周号小于前一天
            elif datetime.weekday(datetime.strptime(day_data_r0v[i][0], "%Y-%m-%d")) < datetime.weekday(datetime.strptime(day_data_r0v[i-1][0], "%Y-%m-%d")):
                
                new_data_row.insert(0, temp_date[-1])#0.日期
                new_data_row.insert(1, temp_name)#1.名称
                new_data_row.insert(2, temp_code)#2.代码
                new_data_row.insert(3, temp_open[0])#3.开盘价
                new_data_row.insert(4, max(temp_price))#4.最高价
                new_data_row.insert(5, min(temp_price))#5.最低价
                new_data_row.insert(6, temp_close[-1])#6.收盘价
                new_data_row.insert(7, sum(temp_turn))
                new_data_row.insert(8, sum(temp_volume))
                new_data_row.insert(9, sum(temp_totalmoney))
                new_data_row.insert(10, temp_totalmarketcap[-1])
                new_data_row.insert(11, temp_circulationmarketcap[-1])

                new_data_row_i = copy.deepcopy(new_data_row)

                new_data_row.clear()

                week_data.append(new_data_row_i)

                temp_date.clear()
                temp_open.clear()
                temp_price.clear()
                temp_close.clear()
                temp_turn.clear()
                temp_volume.clear()
                temp_totalmoney.clear()
                temp_totalmarketcap.clear()
                temp_circulationmarketcap.clear()

            else:

                pass

        return week_data

    # ----------------------------------------------
    # 将按日数据中的列分别取出
    # ----------------------------------------------
    def split_day_data():

        day_data_r0v = data_processing.day_data_remove_0_value()

        day_date_r0v = []

        day_open_r0v = []

        day_high_r0v = []

        day_low_r0v = [] 

        day_close_r0v = []

        day_volume_r0v = []

        for i in day_data_r0v:

            day_date_r0v.append(i[0])

            day_open_r0v.append(i[3])

            day_high_r0v.append(i[4])

            day_low_r0v.append(i[5])

            day_close_r0v.append(i[6])

            day_volume_r0v.append(i[11])

        return day_date_r0v, day_open_r0v, day_high_r0v, day_low_r0v, day_close_r0v, day_volume_r0v

    # ----------------------------------------------
    # 将按周数据中的列分别取出
    # ----------------------------------------------
    def split_week_data():

        week_data = data_processing.day_to_week()

        week_date = []

        week_open = []

        week_high = []

        week_low = [] 

        week_close = []

        week_volume = []

        for i in week_data:

            week_date.append(i[0])

            week_open.append(i[3])

            week_high.append(i[4])

            week_low.append(i[5])

            week_close.append(i[6])

            week_volume.append(i[8])

        return week_date, week_open, week_high, week_low, week_close, week_volume
            
if __name__=='__main__':

    data_processing.split_day_data()

# - END -
